//
//  MainViewController.swift
//  WeiBo
//
//  Created by LTX on 2016/10/29.
//  Copyright © 2016年 LTX. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    //发布按钮
    lazy var composeBtn :UIButton = UIButton(image: "tabbar_compose_icon_add", bgImage: "tabbar_compose_button")

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        setComposeBtn()
        
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
      super . viewWillAppear(animated)
            setTabBar()
    }
    


}

// MARK:- 设置UI
extension MainViewController{
    
    func setTabBar() {
        
        for i in 0..<tabBar.items!.count {
            let item = tabBar.items![i]
            if i == 2 {
                item.isEnabled = false
            }
        }
    }
    
    
    // MARK:- 初始化设置
    func setComposeBtn() -> Void {
        
        addChildViewController(HomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController(), title: "", imageName: "")
        addChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileViewController(), title: "我", imageName: "tabbar_profile")
        
        //添加发布按钮
        tabBar.addSubview(composeBtn)
//        composeBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: .normal)
//        composeBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: .highlighted)
//        composeBtn.setImage(UIImage(named: "tabbar_compose_icon_add"), for: .normal)
//        composeBtn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        
        composeBtn.addTarget(self, action: #selector(tap), for: UIControlEvents.touchUpInside)
        composeBtn.sizeToFit()
        composeBtn.center = CGPoint(x: tabBar.center.x, y: tabBar.bounds.height * 0.5)
        
    }
    
    //发布按钮点击事件
    func tap() {
        print("点击了发布")
    }
    
    
    // swift支持方法重载
    // 方法的重载:方法名称相同，但是参数不同。--> 1.参数类型不同；2.参数的个数不同
    // private在当前文件中可以访问，但在其他文件不能访问
    
    /// <#Description#>
    ///
    /// - parameter childVc:   <#childVc description#>
    /// - parameter title:     <#title description#>
    /// - parameter imageName: <#imageName description#>
    fileprivate func addChildViewController(_ childVc: UIViewController, title: String, imageName: String) {
        
        childVc.title = title
        childVc.tabBarItem.image = UIImage(named: imageName)
        childVc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        let homeNav = UINavigationController(rootViewController: childVc)
        
        addChildViewController(homeNav)
        
    }

    
    
    
}










